var app = getApp();
var https = "https://www.moreclub.cn/";
var http2 = 'http://59.110.53.149:8088/';
var http1 = 'http://10.150.7.248:8001/'
var n=1;
var scrolltop = false
var istrue = false;
var isInto = true;
var mid;

var add0 = function (m) {
  return m < 10 ? '0' + m : m
}
var time = function (prefeerTime) {
  var time = new Date(prefeerTime);
  // var y = time.getFullYear();
  var m = time.getMonth() + 1;
  var d = time.getDate();
  var h = time.getHours();
  var mm = time.getMinutes();
  // var s = time.getSeconds();
  // return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
  return add0(m) + '月' + add0(d) + '日' + ' ' + add0(h) + ':' + add0(mm)
}

var getList = function (that) {
  var uid = wx.getStorageSync('uid');
  wx.request({
    url: https + 'v2/ugc/open/listsbymid?uid=' + uid + '&mid=' + mid + '&page=0' + '&pageSize=20',
    data: {
      page: 0,
      pageSize: 20
    },
    success: function (res) {
      console.log(res)
      if (res.data.success) {
        var pubData = res.data.data.ugcs
        try {
          wx.setStorageSync('pubData', pubData);
        } catch (e) {
        }

        var pubList = [];//创建一个新数组
        for (var key in pubData) {
          var fromThumb = pubData[key].thumb;//头像
          // if (pubData[key].prefeerPictrues) {
          //   var photos = pubData[key].prefeerPictrues;//图片
          //   var photo = photos.split(",");
          // }
          var photo = pubData[key].pictures
          if (pubData[key].content){
            var prefeerContent = pubData[key].content;//内容
            var contentArr = prefeerContent.split('\n');
          }
        
          var nickName = pubData[key].nickName;
          if (pubData[key].likeDto.userLikes) {
            var userLikes = pubData[key].likeDto.userLikes.reverse();//点赞人显示
          } else {
            var userLikes = pubData[key].likeDto.userLikes
          }

          var prefeerTime = pubData[key].createTime;//时间
          var pid = pubData[key].ugcId;
          var isClick = pubData[key].likeDto.clicked;
          var likeTimes = pubData[key].likeDto.likeTimes
          var merchantName = pubData[key].merchantName;
          var chainInter = pubData[key].chainInter;
          //var subType = pubData[key].subType;
          //var sourceFrom = pubData[key].sourceFrom;//more出品
          var uid1 = pubData[key].uid;
          //var type1 = pubData[key].type;
          var commentCount = pubData[key].commentCount;//评论人数
          var comments = pubData[key].comments;//评论内容
          time(prefeerTime)
          var dataTime = time(prefeerTime)

          // for (var i = 0; i < pubData[key].likeDto.userLikes.length;i++){
          //   if (uid == pubData[key].likeDto.userLikes[i].uid) {
          //       reday = true
          //      break;
          //   }else{
          //      reday = false
          //   }
          // }


          var obj = {};//创建新对象
          obj.fromThumb = fromThumb;
          obj.nickName = nickName;
          //obj.prefeerContent = prefeerContent;
          obj.dataTime = dataTime;
          obj.chainInter = chainInter;
          //obj.photos = photos;
          obj.photo = photo;
          obj.isClick = isClick;
          obj.likeTimes = likeTimes;
          obj.merchantName = merchantName;
          //obj.subType = subType;
          // obj.sourceFrom = sourceFrom;
          // obj.type1 = type1;
          obj.pid = pid;
          obj.uid1 = uid1;
          obj.contentArr = contentArr;
          obj.userLikes = userLikes;
          obj.commentCount = commentCount;
          obj.comments = comments;
          pubList.push(obj);
        };
        console.log(pubList)
        if (pubList != null) {

          var oldlist = that.data.totallist;
          for (var i = 0; i < pubList.length; i++) {
            oldlist.push(pubList[i])
          }
        }
        that.setData({
          pubList: pubList,
          totallist: oldlist,
          uid: uid
        })

      }
    }
  })
}

Page({
    data:{
      titles: ["现场动态", "Rapper榜", "点赞排行"],
      color: ["#ffcc00", "#bbd0e5", "#c5ebcc","#fbebb3"],
      // 定义选中标题的初始值0
      selectedTitle: "0",
      scrollTop:0,
      totallist:[]
    },
    onLoad:function(options){
      var that = this
      var midName = options.midName
      var banner = options.banner
      mid = options.mid
      var uid = wx.getStorageSync('uid');

      wx.setNavigationBarTitle({
        title: midName
      })
      that.setData({
        banner:banner,
        n:n,
        color:that.data.color
      })

      wx.getSystemInfo({
        success: (res) => {
          let ww = res.windowWidth;
          let wh = res.windowHeight;
          let scrollH = wh;

          that.setData({
            scrollH: scrollH,
            w: ww,
            wh: wh
          });

        }
      })


      if (!isInto) {
        var total = wx.getStorageSync('total');
        that.setData({
          totallist: total
        })
      } else {
        getList(that)
      }

      //话题排行
      wx.request({
        url: https +'v3/prefeer/open/countByTopics?page=1&pageSize=30&city=2',
        success:function(res){
          console.log(res)
          var resData = res.data.data
          that.setData({
            resData:resData
          })
        }
      })
      //点赞排行
      wx.request({
        url: https +'v3/prefeer/open/clicktime/rankings?page=1&pageSize=30&city=2',
        success:function(res){
          console.log(res)
          var datalist = res.data.data
          that.setData({
            datalist:datalist
          })
        }
      })

    },

   
   
    scroll:function(e){
      console.log(e)
      var that = this;
      var scrollTop = that.data.scrollTop
      
        that.setData({
          scrollTop: e.detail.scrollTop
        })
      
    },
    nav_box:function(){
      wx.navigateTo({
        url: '../detail/detail'
      })
    },

    into_pub:function(){
      wx.navigateTo({
        url: '../dynamics/dynamics?mid='+mid
      })
    },

    //点赞
    praiseBtn: function (e) {
      console.log(e)
      var that = this;
      var uid = wx.getStorageSync('uid');
      var access_token = wx.getStorageSync('inToken');
      var total = that.data.totallist;
      var thumb = wx.getStorageSync('thumb');
      var nickName = wx.getStorageSync('myUsername');
      var idx1 = e.currentTarget.dataset.idx;
      isInto = false

      //  wx.request({
      //    url: https + 'v1/information/aordeliketime?uid=' + uid + '&fid=' + total[idx1].pid + '&type=' + total[idx1].type1 + '&access_token=' + access_token,
      //    success: function (res) {
      //      console.log(res)
      //    }
      //  })

      if (total[idx1].isClick && total[idx1].userLikes != '') {

        for (var j in total[idx1].userLikes) {
          if (total[idx1].userLikes[j].uid == uid) {
            var n = j;
            total[idx1].userLikes.splice(n, 1)

            total[idx1].userLikes = total[idx1].userLikes
            total[idx1].isClick = false
            total[idx1].likeTimes -= 1
            that.setData({
              totallist: total,
              idx1: idx1,
              isClick: total[idx1].isClick
            })
          }
        }
        try {
          wx.setStorageSync('total', total);
        } catch (e) {
        }

      } else {
        if (total[idx1].userLikes) {
          var o = {}
          o.thumb = thumb;
          o.uid = uid;
          o.nickName = nickName;
          total[idx1].userLikes.push(o)
          total[idx1].isClick = true
          total[idx1].likeTimes += 1
          that.setData({
            totallist: total,
            idx1: idx1,
            isClick: total[idx1].isClick
          })
          try {
            wx.setStorageSync('total', total);
          } catch (e) {
          }
        } else {
          var arr = []
          var o = {}
          o.thumb = thumb;
          o.uid = uid;
          o.nickName = nickName;
          arr.push(o)
          total[idx1].userLikes = arr
          total[idx1].isClick = true
          total[idx1].likeTimes += 1
          console.log(total)
          that.setData({
            totallist: total,
            idx1: idx1,
            isClick: total[idx1].isClick
          })
          try {
            wx.setStorageSync('total', total);
          } catch (e) {
          }
        }

      }


    },

    bindtap: function (e) {
      console.log(e)
      this.setData({
        selectedTitle: e.currentTarget.id
      });
    },
    //定义滑块改变的事件处理函数，将current赋值给selectedTitle
    bindChange: function (e) {
      this.setData({
        selectedTitle: e.detail.current
      })
    },
    onReady: function () {
      // 页面渲染完成
      var that = this;
      wx.getSystemInfo({
        success: function (res) {
          that.setData({
            swiperHeight: (res.windowHeight - 37)
          });
        }
      })
    }
 
})